package com.tzinfo.tzb;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.ILoadingLayout;
import com.handmark.pulltorefresh.library.PullToRefreshAdapterViewBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.tzinfo.data.ManagementResult;
import com.tzinfo.data.ManagementResultData;
import com.tzinfo.tzb.view.adapter.ManagementAdapter;
import com.tzinfo.tzt.R;
import com.tzinfo.tzt.util.HttpUtils;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2014/11/21.
 */
public class ManagementActivity extends Activity {
    private PullToRefreshAdapterViewBase listView;
    int pagesize = 10;
    private boolean isLoadingData = false;
    int page=1;//用来判断是否是第一次加载
    private ManagementAdapter adapter;
    List<ManagementResultData> listData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_management);
        listView = (PullToRefreshAdapterViewBase) findViewById(R.id.management_listView);
        listView.setMode(PullToRefreshBase.Mode.BOTH);

        ILoadingLayout endLabels = listView.getLoadingLayoutProxy(false, true);
        endLabels.setPullLabel("上拉加载更多");
        endLabels.setReleaseLabel("松开加载");
        endLabels.setRefreshingLabel("正在加载");

        new GetDataTask().execute();

        listView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                String label = DateUtils.formatDateTime(
                        ManagementActivity.this,
                        System.currentTimeMillis(),
                        DateUtils.FORMAT_SHOW_TIME
                                | DateUtils.FORMAT_SHOW_DATE
                                | DateUtils.FORMAT_ABBREV_ALL);


                if (!isLoadingData) {
                    page = 1;
                    refreshView.getLoadingLayoutProxy()
                            .setLastUpdatedLabel(label);
                    new GetDataTask().execute();
                }

            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                String label = DateUtils.formatDateTime(
                        ManagementActivity.this,
                        System.currentTimeMillis(),
                        DateUtils.FORMAT_SHOW_TIME
                                | DateUtils.FORMAT_SHOW_DATE
                                | DateUtils.FORMAT_ABBREV_ALL);
                // 显示最后更新的时间
                if (!isLoadingData) {
                    refreshView.getLoadingLayoutProxy()
                            .setLastUpdatedLabel(label);
                    new GetDataTask().execute();
                }
            }
        });
    }

    public class GetDataTask extends AsyncTask<String, Void, ManagementResult> {
        protected void onPreExecute() {
            super.onPreExecute();
            if (isLoadingData) {
                this.cancel(true);
                return;
            }
            isLoadingData = true;

        }

        @Override
        protected ManagementResult doInBackground(String... params) {
            List<NameValuePair> pairs = new ArrayList<NameValuePair>();
            pairs.add(new BasicNameValuePair("page", String.valueOf(page)));
            pairs.add(new BasicNameValuePair("pagesize", String.valueOf(pagesize)));
            String GetDataUrl = "http://tzt.tzinfo.net/mob/api/Forum_ForumList.ashx";
            String DataString = HttpUtils.doPostMethod(GetDataUrl, pairs);
            Gson gson = new Gson();
            ManagementResult result = gson.fromJson(DataString, ManagementResult.class);
            return result;
        }

        @Override
        protected void onPostExecute(ManagementResult managementResult) {
            super.onPostExecute(managementResult);
            if (managementResult.getSuccess() == true) {
                if (adapter == null || page == 1) {
                    listData = managementResult.getData();
                } else {
                    listData.addAll(managementResult.getData());
                }
                if (listData.size() == pagesize) {
                    adapter = new ManagementAdapter(ManagementActivity.this, listData);
                    listView.setAdapter(adapter);
                }

                page++;
                adapter.notifyDataSetChanged();
                listView.onRefreshComplete();
                isLoadingData = false;

            } else {
                Toast.makeText(ManagementActivity.this, managementResult.getErrorMsg(), Toast.LENGTH_SHORT)
                        .show();
            }
        }
    }

    public void goback(View view) {
        this.finish();
    }
}
